<?php

//获取业务操作接口
$apiSql = "SELECT 
            ap_id,
            ap_name,
            ap_api,
            ap_method, 
            ap_is_auto,
            ap_type,
            ap_is_cache,
            ap_is_get_cache,
            ap_auto_table,
            ap_cache_type
        FROM sys_action_permissions WHERE ap_api_type = 'BUSSINESS' AND ap_is_delete = '0'";
$apiData = $db->mysqlDB->query($apiSql);

//获取接口请求参数信息
for($i=0;$i<count($apiData);$i++){

    $apid = $apiData[$i]['ap_id'];

    $requestSql = "SELECT rp_name,rp_rule,rp_way FROM sys_request_parameter WHERE rp_apid = $apid AND rp_is_must = '1'";

    $requestData = $db->mysqlDB->query($requestSql);

    if(count($requestData) > 0){
        $apiData[$i]['requestParams'] = $requestData;
    }else{
        $apiData[$i]['requestParams'] = [];
    }
}

//根据数据生成配置文件内容
$apiConfigArr = "";

for($i=0;$i<count($apiData);$i++){

    $apiItem = "";

    $apName        = $apiData[$i]['ap_name'];
    $apApi         = $apiData[$i]['ap_api'];
    $apMethod      = $apiData[$i]['ap_method'];
    $apIsAuto      = $apiData[$i]['ap_is_auto'];
    $apType        = $apiData[$i]['ap_type'];
    $apAutoTable   = $apiData[$i]['ap_auto_table'];
    $apAutoType    = $apiData[$i]['ap_type'];
    $apIsCache     = $apiData[$i]['ap_is_cache'];
    $apIsGetCache  = $apiData[$i]['ap_is_get_cache'];
    $requestParams = $apiData[$i]['requestParams'];

    //判断是否为自动构建
    $autoStr = "";

    if($apIsAuto){
        $autoStr .= "\n\t\t'autoTable' => '$apAutoTable',\n\t\t";
        $autoStr .= "'autoType'  => '$apAutoType',\n\t\t";
    }

    //判断是否缓存
    $cacheStr = "";
    
    if($apIsCache){
        $cacheStr .= "'isCache' => 'YES',\n\t\t";
    }

    if($apIsGetCache){
        $cacheStr .= "'isGetCache' => 'YES',\n\t\t";
    }
    
    //构建请求参数字符串
    $requestParamsStr = "";
    $requestPostParamsStr = "";
    $requestGetParamsStr  = "";

    if(count($requestParams)>0){

        $requestPostParamsStr .= "'postMustParams' => array(\n\t\t\t";
        $requestGetParamsStr .= "\t\t'getMustParams' => array(\n\t\t\t";

        for($k=0;$k<count($requestParams);$k++){

            $rpName = $requestParams[$k]['rp_name'];
            $rpRule = $requestParams[$k]['rp_rule'];
            $rpWay  = $requestParams[$k]['rp_way'];


            if($k == count($requestParams) - 1){
                if($rpWay == 'BODY'){
                    $requestPostParamsStr .= "'$rpName | $rpRule'\n\t\t";
                }else{
                    $requestGetParamsStr .= "'$rpName | $rpRule'\n\t\t";
                }
            }else{
                if($rpWay == 'BODY'){
                    $requestPostParamsStr .= "'$rpName | $rpRule',\n\t\t\t";
                }else{
                    $requestGetParamsStr .= "'$rpName | $rpRule',\n\t\t\t";
                }
            }

        }

        $requestPostParamsStr .= "),\n";
        $requestGetParamsStr  .= ")";

    }

    $apiItem = "\t//$apName\n\t'$apApi' => array(
        'method' => '$apMethod',$autoStr$cacheStr$requestPostParamsStr$requestGetParamsStr
    ),\n";

    $apiConfig .= $apiItem;

}

$apiConfigStr = <<<EOT
<?php
/*
 * 接口配置文件
 */
return array(
    $apiConfig
);
EOT;

if(is_writable('./api/api.php')){
    file_put_contents('./api/api.php',$apiConfigStr);
    $response->responseData(true);
}else{
    $response->responseData(false,"Api文件没有写入权限，请先设置相关权限");
}